package com.xl.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xl.generator.common.ActionResult;
import com.xl.generator.common.ActionResultService;
import com.xl.generator.domain.CommonUser;
import com.xl.generator.domain.CommunityPost;
import com.xl.generator.domain.ReviewRecord;
import com.xl.generator.domain.SubjectiveQuestion;
import com.xl.generator.dto.CommunityPostDto;
import com.xl.generator.mapper.SubjectiveQuestionMapper;
import com.xl.generator.pojo.PageList;
import com.xl.generator.service.CommunityPostService;
import com.xl.generator.service.SubjectiveQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(value = "论坛帖子")
@RestController
@RequestMapping("community")
public class CommunityController {


    @Resource
    private CommunityPostService communityPostService;

    @ApiOperation(value = "分页查询所有帖子", notes="")
    @PostMapping("list")
    public ActionResult test(@RequestParam("current") Integer current, @RequestParam("Size") Integer size,
                             @RequestBody CommunityPost communityPost){
        IPage<CommunityPost> choiceQuestion = new Page<>();
        choiceQuestion.setCurrent(current);
        choiceQuestion.setSize(size);
        if (communityPost.getParentId()==null){
            communityPost.setParentId(-1);
        }
        List<CommunityPostDto> list  = communityPostService.getPostList(choiceQuestion,communityPost.getParentId());
        PageList pageList = new PageList();
        pageList.setTotal(choiceQuestion.getTotal());
        pageList.setData(list);
        return ActionResultService.callBackResultOK(pageList);
    }

    @ApiOperation(value = "查询当前帖子的所有评论", notes="")
    @PostMapping("listComment")
    public ActionResult listComment(@RequestBody CommunityPost communityPost){
        IPage<CommunityPost> choiceQuestion = new Page<>();
        LambdaQueryWrapper<CommunityPost> wrapper = new LambdaQueryWrapper<>();

        IPage<CommunityPost> page = communityPostService.page(choiceQuestion,wrapper.eq(
                CommunityPost::getParentId,communityPost.getId()
        ));
        PageList pageList = new PageList();
        pageList.setData(page);
        pageList.setTotal(pageList.getTotal());
        return ActionResultService.callBackResultOK(pageList);
    }

    @ApiOperation(value = "新增帖子或评论", notes="新增帖子或评论")
    @PostMapping ("insert")
    public ActionResult get(@RequestBody CommunityPost communityPost){

        boolean row = communityPostService.save(communityPost);
        if (row){
            return ActionResultService.callBackResultOK("添加成功");
        }
        return ActionResultService.callBackResultOK("添加失败");
    }

    @ApiOperation(value = "删除帖子或评论", notes="搜索名称查题，支持模糊查询")
    @PostMapping ("search")
    public ActionResult search(@RequestBody CommunityPost communityPost){
        LambdaQueryWrapper<CommunityPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommunityPost::getId,communityPost.getId());
        boolean remove = communityPostService.remove(queryWrapper);
        if (remove){
            return	ActionResultService.callBackResultOK("删除成功");
        }
        return ActionResultService.callBackResultFail("删除失败");
    }

}
